package 树;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ssh
 * @date 2025/8/30
 * @description
 */
public class 前序遍历 {
    public List<Integer> preOrder(TreeNode root) {
        List<Integer> res=new ArrayList<Integer>();
        dfs(res,root);
        return res;
    }
    void dfs(List<Integer> res,TreeNode root){
        if(root==null){
            return ;
        }
        res.add(root.val);
        dfs(res,root.left);
        dfs(res,root.right);
    }

    public static void main(String[] args) {
        // 测试代码
        TreeNode root = new TreeNode(1);
        root.right = new TreeNode(2);
        root.right.left = new TreeNode(3);

        前序遍历 solution = new 前序遍历();
        List<Integer> result = solution.preOrder(root);
        System.out.println(result); // 输出: [1, 2, 3]
    }
}
